package com.example.minefieldkeepercloud.activity.machineslist;

import android.app.Activity;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.minefieldkeepercloud.R;
import com.example.minefieldkeepercloud.adapter.GridBrandAdapter;
import com.example.minefieldkeepercloud.adapter.GridCoinAdapter;
import com.example.minefieldkeepercloud.adapter.GridCustomerAdapter;
import com.example.minefieldkeepercloud.adapter.GridModeAdapter;
import com.example.minefieldkeepercloud.bean.BrandlAndModelBean;
import com.example.minefieldkeepercloud.bean.ByFactoryBean;
import com.example.minefieldkeepercloud.bean.CoinBean;
import com.example.minefieldkeepercloud.util.ToastUtil;
import com.example.minefieldkeepercloud.view.MyGridView;
import java.util.ArrayList;
import java.util.List;

public class MenuControl {

    private Activity mActivity;
    private MyGridView grid_brand, grid_mode, grid_customer,grid_coin;
    private AppCompatEditText editText1, editText2, editText3, editText4, editText5;
    private LinearLayout ly_more1, ly_more2, ly_more3,ly_more4;
    private EditText edit_sl1, edit_sl2, edit_wd1, edit_wd2;
    private TextView tv_cancel, tv_sure;
    private GridBrandAdapter gridBrandAdapter;
    private GridModeAdapter gridModeAdapter;
    private GridCustomerAdapter gridCustomerAdapter;
    private GridCoinAdapter gridCoinAdapter;
    private LinearLayout ly_xh;
    private List<BrandlAndModelBean.DataBean> list_brand = new ArrayList<>();
    private List<CoinBean.DataBean> list_coin = new ArrayList<>();
    private List<BrandlAndModelBean.DataBean.BasemodelBean> list_mode = new ArrayList<>();
    private List<ByFactoryBean.DataBean> list_factory = new ArrayList<>();
    private int list_brand_position=0;
    private boolean bln_mode;
    private boolean bln_brand;
    private boolean bln_customer;
    private boolean bln_coin;
    private ImageView iv_xh,iv_pp,iv_kh,iv_bz;
    private TextView tv_xh,tv_pp,tv_kh,tv_bz;
    private MenuInterface menuInterface;

    private String str_ip1,str_ip2,str_ip3,str_ip4,str_ip5;//IP查询
    private String str_brandId="";//品牌id
    private List<String> list_modeId=new ArrayList<>();//型号id
    private String coinId="";//币种Id
    private String Min_Sl="",Max_Sl="",Min_Wd="",Max_Wd="";//算力和温度的区间值
    private List<String> list_customerId=new ArrayList<>();//型号id
    public MenuControl(Activity mActivity,MenuInterface menuInterface) {
        this.mActivity = mActivity;
        this.menuInterface=menuInterface;
        initLayout();
    }

    private void initLayout() {

        list_brand.clear();
        list_coin.clear();
        list_mode.clear();
        list_factory.clear();
        list_modeId.clear();
        list_customerId.clear();

        editText1 = mActivity.findViewById(R.id.edit1);
        editText2 = mActivity.findViewById(R.id.edit2);
        editText3 = mActivity.findViewById(R.id.edit3);
        editText4 = mActivity.findViewById(R.id.edit4);
        editText5 = mActivity.findViewById(R.id.edit5);
        grid_brand = mActivity.findViewById(R.id.grid_brand);
        grid_mode = mActivity.findViewById(R.id.grid_mode);
        grid_customer = mActivity.findViewById(R.id.grid_customer);
        grid_coin = mActivity.findViewById(R.id.grid_cion);
        ly_more1 = mActivity.findViewById(R.id.ly_more1);
        ly_more2 = mActivity.findViewById(R.id.ly_more2);
        ly_more3 = mActivity.findViewById(R.id.ly_more3);
        ly_more4= mActivity.findViewById(R.id.ly_more4);
        edit_sl1 = mActivity.findViewById(R.id.edit_sl1);
        edit_sl2 = mActivity.findViewById(R.id.edit_sl2);
        edit_wd1 = mActivity.findViewById(R.id.edit_wd1);
        edit_wd2 = mActivity.findViewById(R.id.edit_wd2);
        tv_cancel = mActivity.findViewById(R.id.tv_cancel);
        tv_sure = mActivity.findViewById(R.id.tv_sure);
        ly_xh = mActivity.findViewById(R.id.ly_xh);
        iv_xh=mActivity.findViewById(R.id.iv_xh);
        iv_pp=mActivity.findViewById(R.id.iv_pp);
        iv_kh=mActivity.findViewById(R.id.iv_kh);
        iv_bz=mActivity.findViewById(R.id.iv_bz);
        tv_xh=mActivity.findViewById(R.id.tv_xh);
        tv_pp=mActivity.findViewById(R.id.tv_pp);
        tv_kh=mActivity.findViewById(R.id.tv_kh);
        tv_bz=mActivity.findViewById(R.id.tv_bz);

        gridBrandAdapter = new GridBrandAdapter(mActivity, list_brand);
        grid_brand.setAdapter(gridBrandAdapter);

//        editText1.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//               if(s.length()==0)
//               {
//                   editText1.setText("0");
//               }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
//        editText2.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if(s.length()==0)
//                {
//                    editText2.setText("0");
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
//        editText3.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if(s.length()==0)
//                {
//                    editText3.setText("0");
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
//        editText4.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if(s.length()==0)
//                {
//                    editText4.setText("0");
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
//        editText5.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                if(s.length()==0)
//                {
//                    editText5.setText("0");
//                }
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//
//            }
//        });
        grid_brand.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridBrandAdapter.setSelected(position);
                str_brandId=list_brand.get(position).getBrandlID();
                ly_xh.setVisibility(View.VISIBLE);
                grid_mode.setVisibility(View.VISIBLE);
                list_brand_position=position;
                list_mode=list_brand.get(position).getBasemodel();
                if(bln_mode)
                gridModeAdapter.isShow(false,list_brand.get(position).getBasemodel());
                else
                gridModeAdapter.isShow(true,list_brand.get(position).getBasemodel());
            }
        });

        gridModeAdapter = new GridModeAdapter(mActivity, list_mode);
        grid_mode.setAdapter(gridModeAdapter);

        grid_mode.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridModeAdapter.setSelected(position+"");

                if(list_modeId.contains(list_mode.get(position).getID()+""))
                {
                    list_modeId.remove(list_mode.get(position).getID()+"");
                }
                else
                {
                    list_modeId.add(list_mode.get(position).getID()+"");
                }
            }
        });
//
        gridCustomerAdapter = new GridCustomerAdapter(mActivity, list_factory);
        grid_customer.setAdapter(gridCustomerAdapter);

        grid_customer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridCustomerAdapter.setSelected(position+"");
                if(list_customerId.contains(list_factory.get(position).getID()+""))
                {
                    list_customerId.remove(list_factory.get(position).getID()+"");
                }
                else
                {
                    list_customerId.add(list_factory.get(position).getID()+"");
                }
            }
        });

        gridCoinAdapter=new GridCoinAdapter(mActivity,list_coin);
        grid_coin.setAdapter(gridCoinAdapter);

        grid_coin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                gridCoinAdapter.setSelected(position);
                coinId=list_coin.get(position).getID()+"";
            }
        });


        ly_more1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//品牌的更多
                if(!bln_brand)//展开
                {
                    gridBrandAdapter.isShow(false,list_brand);
                    iv_pp.animate().rotation(180);
                    tv_pp.setText("收起");
                    bln_brand=true;
                }
                else//收起
                {
                    gridBrandAdapter.isShow(true,list_brand);
                    iv_pp.animate().rotation(0);
                    tv_pp.setText("更多");
                    bln_brand=false;
                }

            }
        });




        ly_more2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//型号的更多
                if(!bln_mode)//展开
                {
                    gridModeAdapter.isShow(false,list_brand.get(list_brand_position).getBasemodel());
                    iv_xh.animate().rotation(180);
                    tv_xh.setText("收起");
                    bln_mode=true;
                }
                else//收起
                {
                    gridModeAdapter.isShow(true,list_brand.get(list_brand_position).getBasemodel());
                    iv_xh.animate().rotation(0);
                    tv_xh.setText("更多");
                    bln_mode=false;
                }

            }
        });

        ly_more3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//客户的更多
                if(!bln_customer)//展开
                {
                    gridCustomerAdapter.isShow(false,list_factory);
                    iv_kh.animate().rotation(180);
                    tv_kh.setText("收起");
                    bln_customer=true;
                }
                else//收起
                {
                    gridCustomerAdapter.isShow(true,list_factory);
                    iv_kh.animate().rotation(0);
                    tv_kh.setText("更多");
                    bln_customer=false;
                }

            }
        });
        ly_more4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//型号的更多
                if(!bln_coin)//展开
                {
                    gridCoinAdapter.isShow(false,list_coin);
                    iv_bz.animate().rotation(180);
                    tv_bz.setText("收起");
                    bln_coin=true;
                }
                else//收起
                {
                    gridCoinAdapter.isShow(true,list_coin);
                    iv_bz.animate().rotation(0);
                    tv_bz.setText("更多");
                    bln_coin=false;
                }

            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuInterface.cancel();
            }
        });

        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                str_ip1=editText1.getText().toString();
                str_ip2=editText2.getText().toString();
                str_ip3=editText3.getText().toString();
                str_ip4=editText4.getText().toString();
                str_ip5=editText5.getText().toString();

                if(!str_ip1.isEmpty()||!str_ip2.isEmpty()||!str_ip3.isEmpty()||!str_ip4.isEmpty()||!str_ip5.isEmpty())
                {
                    if(!str_ip1.isEmpty()&&!str_ip2.isEmpty()&&!str_ip3.isEmpty()&&!str_ip4.isEmpty()&&!str_ip5.isEmpty())
                    {

                    }
                    else
                    {
                        ToastUtil.showToast(mActivity,"请填写完整的IP地址");
                        return;
                    }
                }

                Min_Sl=edit_sl1.getText().toString();
                Max_Sl=edit_sl2.getText().toString();
                Min_Wd=edit_wd1.getText().toString();
                Max_Wd=edit_wd2.getText().toString();
                menuInterface.sure(str_ip1,str_ip2,str_ip3,str_ip4,str_ip5,str_brandId,list_modeId,coinId,Min_Sl,Max_Sl,Min_Wd,Max_Wd,list_customerId);
            }
        });
    }

    public void setBrandData(List<BrandlAndModelBean.DataBean> list_brand) {
        this.list_brand = list_brand;
        gridBrandAdapter.isShow(true,list_brand);
    }

    public void setCustomer(List<ByFactoryBean.DataBean> list_factory) {
        this.list_factory=list_factory;
        gridCustomerAdapter.isShow(true,list_factory);
    }

    public void setCoinData(List<CoinBean.DataBean> list_coin) {
        this.list_coin = list_coin;
        gridCoinAdapter.isShow(true,list_coin);
    }

    public interface MenuInterface
    {
        void cancel();
        void sure(String str_ip1,String str_ip2,String str_ip3,String str_ip4,String str_ip5,String str_brandId,List<String> list_modeId,String coinId
        ,String Min_Sl,String Max_Sl,String Min_Wd,String Max_Wd,List<String> list_customerId);
    }
}
